<?php
namespace Services;

use Helpers\HttpClient;
use Helpers\Main;
use Helpers\Tool;
use Helpers\XhException;

class Fiction {

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    public static $article = [
        'actionType' => [1 => '屏蔽文章', 2 => '恢复文章', 3 => '删除文章', 4 => '及格', 5 => '良好', 6 => '优秀']
    ];
    public static $comment = [
        'actionType' => [1 => '屏蔽评论', 2 => '恢复评论', 3 => '删除评论', 4 => '不通过评论', 5 => '通过评论']
    ];
    /**
     * 初始化
     *
     * @throws XhException
     *
     * create by dale  20180725
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('admire');
            self::$url  = Tool::getValue($config, 'url');
//            self::$url = '192.168.8.38:9100';
            if (!self::$url) {
                throw new XhException('学海美文url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 获取文章
     *
     * @param int $page
     * @param int $size
     * @param array $params
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180726
     */
    public static function getArticles($page = 1, $size = 50, $params = [])
    {
        self::init();
        $queParams = [
            'page' => $page-1,
            'pageSize' => $size
        ];
        $queParams = array_merge($queParams, $params);
        $response = self::$httpClient->postJson('/api/v1/manage/articles', $queParams);
//        var_dump($response);die;
        if (!$response->isSuccess()) {
            throw new XhException('调取接口失败', $response);
        }
        $body = $response->toArray();
        if (!$body || $body['totalCount'] === null || $body['data'] === null) {
            throw new XhException('无返回数据', $response);
        }
        $total = $body['totalCount'];
        $data  = $body['data'];
        return ['total' => $total, 'rows' => $data];
    }

    /**
     * 处理文章
     *
     * @param $action
     * @param array $ids
     * @param array $range
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180730
     */
    public static function dealArticle($action, $ids = [], $range = 0)
    {
        self::init();
        $response = self::$httpClient->postJson('/api/v1/manage/articles/control', [
            'actionType'  => $action,
            'articleId'   => $ids,
            'operationId' => Main::getLoginUserInfo('userId'),
            'operationName' => Main::getLoginUserInfo('userName'),
            'displayRange'  => $range
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('调取' . self::$article['actionType'][$action] . '接口失败', $response);
        }
        return ['code' => 200 , 'msg' => self::$article['actionType'][$action] .'成功'];
    }

    /**
     * 获取文章详情
     *
     * @param $id
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20180730
     */
    public static function getArticleDetail($id){
        self::init();
        $response = self::$httpClient->get(sprintf('/api/v1/manage/%s/articles', $id), []);
        if (!$response->isSuccess()) {
            throw new XhException('调取文章详情接口失败', $response);
        }
        return $response->toArray();
    }

    /**
     * 获取评论
     *
     * @param int $page
     * @param int $size
     * @param array $params
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180731
     */
    public static function getComment($page = 1, $size = 50, $params = []){
        self::init();
        $queParams = [
            'page' => $page-1,
            'pageSize' => $size
        ];
        $queParams = array_merge($queParams, $params);
        $response = self::$httpClient->postJson('/api/v1/manage/comments', $queParams);
        if (!$response->isSuccess()) {
            throw new XhException('调取接口失败', $response);
        }
        $body = $response->toArray();
        if (!$body || $body['totalCount'] === null || $body['data'] === null){
            return ['total' => 0, 'rows' => []];
        }
        $total = $body['totalCount'];
        $data  = $body['data'];
        return ['total' => $total, 'rows' => $data];
    }

    /**
     * 修改评论状态
     *
     * @param $action
     * @param array $ids
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180731
     */
    public static function dealComment($action, $ids = []){
        self::init();
        $response = self::$httpClient->postJson('/api/v1/manage/comments/control', [
            'actionType'  => $action,
            'commentIds'   => $ids,
            'operationId' => Main::getLoginUserInfo('userId'),
            'operationName' => Main::getLoginUserInfo('userName')
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('调取' . self::$comment['actionType'][$action] . '接口失败', $response);
        }
        return ['code' => 200 , 'msg' => self::$comment['actionType'][$action] .'成功'];
    }

    /**
     * 改变星级
     *
     * @param $id
     * @param $star
     *
     * @return array
     * @throws XhException
     *
     * create by dale  20180731
     */
    public static function changeStar($id, $star) {
        self::init();
        if(!is_array($id)) {
            $id = [$id];
        }
        $response = self::$httpClient->postJson('/api/v1/manage/comments/control', [
            'commentIds'   => $id,
            'commentStar' => $star,
            'actionType'  => 5,
            'operationId' => Main::getLoginUserInfo('userId'),
            'operationName' => Main::getLoginUserInfo('userName')
        ]);
        if (!$response->isSuccess()) {
            throw new XhException('调取改变星级接口失败', $response);
        }
        return ['code' => 200 , 'msg' => '改变星级成功'];
    }

    /**
     * 获取评论详情
     *
     * @param $id
     *
     * @return array|bool|mixed|\stdClass
     * @throws XhException
     *
     * create by dale  20180731
     */
    public static function getCommentDetail($id) {
        self::init();
        $response = self::$httpClient->get(sprintf('/api/v1/manage/%s/comment', $id), []);
        if (!$response->isSuccess()) {
            throw new XhException('调取评论详情接口失败', $response);
        }
        return $response->toArray();
    }

}